/*
 * Copyright (c) 2011 Norbert Kopcsek
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.modulr.service {

	import mx.collections.ArrayList;
	import mx.collections.IList;

	import org.modulr.bundle.Bundle;
	import org.modulr.bundle.IBundleContext;
	import org.modulr.framework.Framework;

	/**
	 * A registered service.
	 *
	 * The Framework returns a ServiceRegistration object when a IBundleContext.publishService method invocation is successful.
	 * The ServiceRegistration object is for the private use of the registering bundle and should not be shared with other bundles.
	 * The ServiceRegistration object may be used to update the properties of the service or to unregister the service.
	 *
	 * @author Norbert Kopcsek
	 */
	public class ServiceRegistration implements IServiceRegistration {


		//------------------------------------------------------------------------------
		//
		//   Constructor 
		//
		//------------------------------------------------------------------------------

		public function ServiceRegistration( context:IBundleContext, clazz:String, service:Object, properties:Object = null ) {
			this.state = ServiceRegistrationState.REGISTERED;
			this.service = service;
			this.framework = context.framework;

			_context = context;
			_clazz = clazz;
			_reference = new ServiceReference( this );
			_properties = properties;
			_usingBundles = new ArrayList;
		}


		//------------------------------------------------------------------------------
		//
		//   Properties (Getter/Setter) 
		//
		//------------------------------------------------------------------------------

		//--------------------------------------
		// context 
		//--------------------------------------

		public function get context():IBundleContext {
			return _context;
		}

		//--------------------------------------
		// clazz 
		//--------------------------------------

		public function get clazz():String {
			return _clazz;
		}

		//--------------------------------------
		// reference 
		//--------------------------------------

		public function get reference():IServiceReference {
			if ( _reference == null ) {
				throw new Error( "Service already unregistered." );
			}
			return _reference;
		}

		//--------------------------------------
		// properties 
		//--------------------------------------

		public function get properties():Object {
			return _properties;
		}

		//--------------------------------------
		// bundle 
		//--------------------------------------

		public function get bundle():Bundle {
			return _context.bundle;
		}

		//--------------------------------------
		// usingBundles 
		//--------------------------------------

		public function get usingBundles():IList {
			return _usingBundles;
		}


		//------------------------------------------------------------------------------
		//
		//   Protected fields 
		//
		//------------------------------------------------------------------------------

		/**
		 * @private
		 */
		protected var service:Object;

		/**
		 * @private
		 */
		protected var framework:Framework;

		/**
		 * @private
		 */
		protected var state:String;

		/**
		 * @private
		 */
		protected var _context:IBundleContext;

		/**
		 * @private
		 */
		protected var _clazz:String;

		/**
		 * @private
		 */
		protected var _reference:IServiceReference;

		/**
		 * @private
		 */
		protected var _properties:Object;

		/**
		 * @private
		 */
		protected var _usingBundles:IList;


		//------------------------------------------------------------------------------
		//
		//  Methods 
		//
		//------------------------------------------------------------------------------

		public function unregister():void {
			var reference:IServiceReference = this.reference;

			framework.serviceRegistry.unregisterService( context, this );
			state = ServiceRegistrationState.UNREGISTERING;
			framework.dispatchEvent( new ServiceEvent( ServiceEvent.SERVICE_UNREGISTERING, reference ));


			_reference = null;
			state = ServiceRegistrationState.UNREGISTERED;
			framework.dispatchEvent( new ServiceEvent( ServiceEvent.SERVICE_UNREGISTERED, reference ));
		}

		/**
		 * Returns the actual service. Adds the bundle to an internal list.
		 * @param context The bundle getting the service.
		 */
		public function getService( context:IBundleContext ):Object {
			/*var servicesInUse:Dictionary = context.servicesInUseMap;
			   if( servicesInUse == null ) {
			   throw new Error("BundleContext no valid"); // TODO: Get some proper explaination
			 }*/
			_usingBundles.addItem( context.bundle );
			return service;
		}

		/**
		 * If the service is no longer need, a bundle can unget/return it using this method.
		 * @param context The bundle ungetting the service.
		 * @returns Returns whether or not the service could be unget.
		 */
		public function ungetService( context:IBundleContext ):Boolean {
			releaseService( context );

			var index:int = _usingBundles.getItemIndex( context.bundle );

			if ( index >= 0 ) {
				_usingBundles.removeItemAt( index );
			}
			return true;
		}


		//------------------------------------------------------------------------------
		//
		//   Protected methods 
		//
		//------------------------------------------------------------------------------

		/**
		 * @private
		 */
		protected function releaseService( context:IBundleContext ):void {

		}
	}
}

